package com.lzs.kucun.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.*;
import com.lzs.kucun.service.StockService;


import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpy
 * @since 2020-11-25
 */
@RestController

@RequestMapping("/kucun/stock")
public class StockController {
    @Autowired
    private StockService stockService;
    @ApiOperation(value = "查询库存")
    @PostMapping("/selectStockPage/{current}/{size}")
    public R selectStockPage(@PathVariable Integer current, @PathVariable Integer size, @RequestBody Stock stock,HttpServletRequest request) {
        Page<Stock> stockPage = null;
        try {
            String warehouseId = request.getHeader("warehouseId");
            String[] warehouseid = warehouseId.split(",");
            System.out.println(stock);
            stockPage = stockService.selectStockPage(current, size,stock,warehouseid);

            return R.ok().message("查询成功").data("StockPage", stockPage);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return R.error().message("查询出错").data("StockPage", stockPage);
        }
    }

    /**
     * @param
     * qty:数量
     *            warehouseid: 仓库Id
     *            commoditybar: 商品编码
     *            locatorcode： 货位编码
     * @return
     */
    @ApiOperation(value = "出库锁定数量,并减可用数量")
    @PostMapping("/updateQuantity")
    public R updateQuantity(Long locatorid,Long commodityid,Long warehouseid,Integer qty) {
        return stockService.updateStockQuantity(locatorid,commodityid,warehouseid,qty);
    }

    @ApiOperation(value = "查询货位的商品信息")
    @PostMapping("/selectCommodity/{locatorid}")
    public List<Stock> select(@PathVariable Long locatorid) {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorid",locatorid);
        return stockService.list(queryWrapper);
    }


    /**
     * @param
     * qty:数量
     *            warehouseid: 仓库Id
     *            commoditybar: 商品编码
     *            locatorcode： 货位编码
     * @return
     */
    @ApiOperation(value = "出库锁定数量库存释放")
    @PostMapping("/updateRealQuantity")
    public R updateRealQuantity(Long locatorid,Long commodityid,Long warehouseid,Integer qty) {

        try {
            Stock stock = new Stock();
            QueryWrapper<Stock> stockQueryWrapper = new QueryWrapper<>();
            stockQueryWrapper.eq("warehouseid", warehouseid);
            stockQueryWrapper.eq("commodityid", commodityid);
            stockQueryWrapper.eq("locatorid", locatorid);
            Stock stock1 = stockService.list(stockQueryWrapper).get(0);
            Integer lqty = stock1.getLqty() - qty;
            int cqty = stock1.getCqty() - qty;
            stock.setLqty(lqty);
            stock.setCqty(cqty);
            stockService.update(stock, stockQueryWrapper);
            return R.ok().message("真实减库存成功");
        } catch (RuntimeException e) {
            return R.error().message("真实减库存失败");
        }

    }
    @ApiOperation(value = "取消，锁定数量减,可用数量加")
    @PostMapping("/updateRealQuantity2")
    public R updateRealQuantity2(Long locatorid,Long commodityid,Long warehouseid,Integer qty) {
        try {
            Stock stock = new Stock();
            QueryWrapper<Stock> stockQueryWrapper = new QueryWrapper<>();
            stockQueryWrapper.eq("warehouseid", warehouseid);
            stockQueryWrapper.eq("commodityid", commodityid);
            stockQueryWrapper.eq("locatorid", locatorid);
            Stock stock1 = stockService.list(stockQueryWrapper).get(0);
            Integer lqty = stock1.getLqty() -qty;
            int cqty = stock1.getUqty() + qty;
            stock.setLqty(lqty);
            stock.setUqty(cqty);
            stockService.update(stock, stockQueryWrapper);
            return R.ok().message("取消成功");
        } catch (RuntimeException e) {
            return R.error().message("取消失败");
        }
    }


    /**
     *
     *            locatorcode:库位编码
     *            warehouseid：仓库id
     *            commoditybar：商品编码
     *            cqty：数量
     *            billno:入库单号
     * @return
     */


    @ApiOperation(value = "入库审核")
    @PostMapping("/addStock")
    public R addStock(@RequestBody  Stock stock) {
        try {
            String locatorcode = stock.getLocatorcode();
            Long warehouseid = stock.getWarehouseid();
            String commoditybar = stock.getCommoditybar();
            Integer qty = stock.getCqty();
            Boolean a = stockService.selectIsExit(locatorcode, commoditybar, warehouseid);
            if (!a) {
                stock.setCqty(qty);
                stock.setUqty(qty);
                stock.setLqty(0);
                Locator locator = stockService.selectLocator(locatorcode);
                Commodity commodity = stockService.selectCommodity(commoditybar);
                stock.setLocatorid(locator.getId());
                stock.setLocatorname(locator.getName());
                stock.setCommodityid(commodity.getId());
                stock.setCommodityname(commodity.getName());
                stockService.save(stock);
            } else {
                stockService.updateAllQuantity(locatorcode, commoditybar, warehouseid, qty);
            }
            return R.ok().message("入库成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return R.error().message("入库失败");
        }
    }

    /**
     * 属性
     *commoditybar
     * locatorcode
     * warehouseid
     * cqty：数量
     *  billno:入库单号
     * @param stock
     * @return
     */
        @ApiOperation(value = "入库取消审核")
        @PostMapping("/reduceStock")
        public R reduceStock(@RequestBody Stock stock){
            try {
                Integer cqty = stock.getCqty();
                String commoditybar = stock.getCommoditybar();
                String locatorcode = stock.getLocatorcode();
                Long warehouseid = stock.getWarehouseid();
                stockService.reduceQuantity(locatorcode,commoditybar,warehouseid,cqty);
                return R.ok().message("取消审核成功");
            } catch (RuntimeException e) {
                e.printStackTrace();
                return R.error().message("取消审核失败");
            }

        }

    @GetMapping("searchAlwarehouse")
    @ApiOperation("查询所有仓库")
    public R searchAlwarehouse(){
        try {
            List<Stock> list = stockService.list();
            Set set = new HashSet();
            for (int i = 0; i < list.size(); i++) {
                set.add(list.get(i));
            }
            return R.ok().message("成功").data("set",set);
        }catch (RuntimeException e){
            return R.error().message("失败");
        }
    }
    @GetMapping("findLoactorByWaId/{id}")
    @ApiOperation("查询仓库下所有库位")
    public R findLoactorByWaId(@PathVariable("id") Long warshouseid){
        QueryWrapper<Stock> wrapper=new QueryWrapper();
        wrapper.eq("warehouseid",warshouseid);
        List<Stock> list = stockService.list(wrapper);
        List<Stock> list1=new ArrayList();
        for (int i = 0; i <list.size() ; i++) {
            boolean flag=false;
            for (int j = 0; j <list1.size() ; j++) {
                if (list.get(i).getLocatorid().equals(list1.get(j).getLocatorid())){
                    flag=true;
                    break;
                }
            }
            if (!flag){
                list1.add(list.get(i));
            }
        }
        return R.ok().message("成功").data("list",list1);
    }


    @GetMapping("findCommByLOId/{id}")
    @ApiOperation("查询库位下所有商品")
    public R findCommByLOId(@PathVariable("id") Long locatorid){
        QueryWrapper<Stock> wrapper=new QueryWrapper();
        wrapper.eq("locatorid",locatorid);
        List<Stock> list = stockService.list(wrapper);
        return R.ok().message("成功").data("list",list);
    }







}


